<?php
defined('IN_MAIN') or exit('Invalid Access!');

function print_stockist_invoice($r_sale){
/*
This function will format the invoice output ready to print
*/

	global $vars;
	$country_code=$vars['common']['country_code'];
	array_shift($country_code);
	$country_code_t=$vars['common']['country_code_text'];
	foreach($country_code_t as $code=>$cname){
		$country_code_d[$code]=__($cname);
	}
	$r_payment_d = $vars['dbv']['sale_payment_d'];
	
	if($r_sale["stockist_sale"] == 'y'){
		$this_stockist = get_stockist_detail_by_id($r_sale['ssid']);
		$pickup_location=__("Stockist ID").__(":")." $this_stockist[sid], $this_stockist[name]<br />\n$r_sale[collect_address]".($r_sale['collect_city']? "<br />\n$r_sale[collect_city]" : '').($r_sale['collect_zip'] || $r_sale['collect_state']? "<br />" : "").($r_sale['collect_zip']? "$r_sale[collect_zip], " : "").($r_sale['collect_state']? "$r_sale[collect_state]" : "")."<br />\n".$r_country_d[$r_sale["collect_country"]];
	}else{
		$pickup_location=__("Headquater");
	}
	$r_stockist = get_stockist_detail_by_id($r_sale['sid']);
	$dprods=prepare_prod_for_display_from_ssid($r_sale['id']);
	$formatted_prod=format_prod_display_invoice($dprods, $r_sale['delivery_cost']);
	$product_details="
	<tr>
		<td colspan='2'>$formatted_prod</td>
	</tr>";
	
	$invoice = "
	<table class='print-header'>
		<tr>
			<td width='120'><img src='".M_URL."/images/print/logo.jpg' /></td>
			<td>Guang Fang International Marketing (M) Sdn. Bhd<br />
				1st Floor, No. 6, Jalan Salung 33/26<br />
				Shah Alam Technology Park<br />
				Section 33, 40400 Shah Alam<br />
				Selangor Darul Ehsan, Malaysia.<br />
				Tel: +603 51214278 / 6 Fax:	+603 51214197<br />
				Website : www.gfi2u.com
			</td>
			<td style='text-align:right; font-size:18px; font-weight:bold;'>INVOICE</td>
		</tr>
	</table>
	<table class='order-detail'>
		<tr>
			<td width='50%'>
				<table>
					<tr>
						<td width='120'>Stockist ID:</td>
						<td>$r_stockist[sid]</td>
					</tr>
					<tr>
						<td>Name:</td>
						<td>$r_stockist[name]</td>
					</tr>
					<tr>
						<td>Address:</td>
						<td>$r_stockist[address]".($r_stockist['address2']? '<br />'.$r_stockist['address2'] : '').($r_stockist['city']? '<br />'.$r_stockist['city'] : '').($r_stockist['city'] || $r_stockist['state']? '<br />' : '').($r_stockist['zip']? $r_stockist['zip'].', ' : '').($r_stockist['state']? $r_stockist['state'] : '')."<br />".$country_code_d[$r_stockist['country']]."</td>
					</tr>
					<tr>
						<td>Contact:</td>
						<td>$r_stockist[mobileno]</td>
					</tr>".($r_stockist['email']? "
					<tr>
						<td>Email:</td>
						<td>$r_stockist[email]</td>
					</tr>" : "")."
				</table>
			</td>
			<td width='50%'>
				<table>
					<tr>
						<td>Invoice No.:</td>
						<td><b>$r_sale[refno]</b></td>
					</tr>
					<tr>
						<td>Date:</td>
						<td>".date($vars['date_format'], strtotime($r_sale['cdate']))."</td>
					</tr>
					<tr>
						<td width='120'>Pickup Location:</td>
						<td>$pickup_location</td>
					</tr>
					<tr>
						<td>Payment Type:</td>
						<td>".$r_payment_d[$r_sale['payment_type']]."</td>
					</tr>
					<tr>
						<td>Currency:</td>
						<td>$vars[currency]</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	$product_details
	<p>Remark: $r_sale[remark]</p>
	<table class='signature'>
		<tr>
			<td width='50%'>Issued By:</td>
			<td align='right'>Received By:</td>
		</tr>
		<tr>
			<td><div class='issue-by'></div></td>
			<td><div class='receive-by'></div></td>
		</tr>
	</table>";
	$invoice = "<body style='margin:0px; padding:0px;' onload='window.print();'>$invoice</body></html>";
	
	$print_css = "
	<style type='text/css'>
	td, th, p{
		font-family:arial;
		font-size:13px;
		vertical-align:top;
	}
	table.order-detail, table.print-header, table.product-detail, table.signature{
		width:100%;
		border-collapse:collapse;
		margin-bottom:10px;
	}
	
	table.product-detail td, table.product-detail th{
		border:1px solid #000;
		font-size:12px;
	}
	table.product-detail th{
		background:#bbb;
	}
	div.issue-by, div.receive-by{
		width:180px;
		height:100px;
		border:0px solid #000;
		border-width:0 0 1px 0;
	}
	div.receive-by{
		float:right;
	}
	@page {
		.print-table {
			size:10cm 8cm;
			margin:0cm;
		}
	}
	</style>";
	
	$header = "
	<html>
	<head>
	<title>$vars[title] - Stockist Invoice</title>
	$print_css
	</head>";
	
	return array('invoice'=>$invoice, 'css'=>$header);
}
?>